﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Filters;

namespace OpenSearchEngine
{
    public class HtmlParser
    {
        public static String GetCharset(String content)
        {
            const String CHARSET_STRING = "charset";
            int index;
            String ret;
            ret = null;
            if (null != content)
            {
                index = content.IndexOf(CHARSET_STRING);
                if (index != -1)
                {
                    content = content.Substring(index + CHARSET_STRING.Length).Trim();
                    if (content.StartsWith("="))
                    {
                        content = content.Substring(1).Trim();
                        index = content.IndexOf(";");
                        if (index != -1)
                            content = content.Substring(0, index);//remove any double quotes from around charset string
                        if (content.StartsWith("\"") && content.EndsWith("\"") && (1 < content.Length))
                            content = content.Substring(1, content.Length - 1);
                        //remove any single quote from around charset string
                        if (content.StartsWith("'") && content.EndsWith("'") && (1 < content.Length))
                            content = content.Substring(1, content.Length - 1);
                        ret = content;
                    }
                }
            }
            return (ret);
        }
        /// <summary>
        /// 利用HtmlParser.Net得到编码
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static String GetCharsetFromMeta(string content)
        {
            string result = "";
            Lexer lexer = new Lexer(content);//Lexer包含了词法分析的代码；
            Parser parser = new Parser(lexer);//解析器
            NodeFilter filter = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.MetaTag));//节点过滤器
            NodeList htmlNodes = parser.Parse(filter);//使用节点过滤得到NodeList
            /* 解析之后，我们可以采用： 
             * INode[] nodes = nodeList.toNodeArray();
             * 来获取节点数组，也可以直接访问：
             * INode node = nodeList.elementAt(i);
             * 来获取Node。
             * 另外，在Filter后得到NodeList以后，我们仍然可以使用
             * nodeList.extractAllNodesThatMatch(someFilter)
             * 来进一步过滤，同时又可以用
             * nodeList.visitAllNodesWith(someVisitor)来做进一步的访问。
             */
            for (int i = 0; i < htmlNodes.Count; i++)
            {

                ITag tag = htmlNodes[i] as ITag;
                if (tag != null)
                {

                    string charset = GetCharset(tag.GetAttribute("content"));
                    if (!string.IsNullOrEmpty(charset))
                        return charset;

                }
            }
            return result;
        }
        public static String GetContentType(string htmlText)
        {
           
            string result = "";
            Lexer lexer = new Lexer(htmlText);//Lexer包含了词法分析的代码；
            Parser parser = new Parser(lexer);//解析器
            NodeFilter filter = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.MetaTag));//节点过滤器
            NodeList htmlNodes = parser.Parse(filter);//使用节点过滤得到NodeList
            for (int i = 0; i < htmlNodes.Count; i++)
            {

                ITag tag = htmlNodes[i] as ITag;
                if (tag != null)
                {

                    string httpequiv = tag.GetAttribute("http-equiv");
                    if (!string.IsNullOrEmpty(httpequiv) && httpequiv.ToUpper() == @"CONTENT-TYPE")
                    {
                        return tag.GetText();
                    }
                }
            }
            return result;
        }
        private static ITag getTag(INode node)
        {
            if (node == null)
                return null;
            return node is ITag ? node as ITag : null;
        }

    }
}
